package tests.multiThreadTests.smoker;

import tests.multiThreadTests.MySemaphore;
import java.util.*;

enum RESOURCES{PAPER,GLUE,TOBACCO};

public class Table {
	Producer producer;
	Smoker[] smokers;
	RESOURCES resource;
	MySemaphore<Thread,RESOURCES> semaphore;
	public static void main(String[] args) {
		new Table();
	}
	Table(){
		init();
		producer.start();
		for (Smoker smoker:smokers)
			smoker.start();
	}
	private void init(){
		resource=null;
		producer=new Producer();
		smokers=new Smoker[3];
		for(int i=0;i<smokers.length;i++)
			smokers[i]=new Smoker(RESOURCES.values()[i]);
		LinkedList<Thread> processes=new LinkedList<>();
		processes.add(producer);
		Collections.addAll(processes, smokers);
		semaphore=new MySemaphore<>(new LinkedList<>(),processes,1);
	}
	class Producer extends Thread {
		int resourceNum;

		Producer() {
			resourceNum = 0;
		}

		@Override
		public void run() {
			while (true) {
				resource = RESOURCES.values()[resourceNum];
				System.out.println("produced"+resource);

				resourceNum = (resourceNum + 1) % (RESOURCES.values().length);
				semaphore.wait(this);
			}
		}
	}
	class Smoker extends Thread{
		RESOURCES resources;
		final RESOURCES needed;

		public Smoker(RESOURCES needed) {
			this.needed = needed;
		}

		@Override
		public void run() {
			while (true){

			}
		}
	}
}
